<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/jsp/taglib.jsp"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<%@ include file="/jsp/head.jsp"%>
	<link rel="stylesheet" href="${ctx}/assets/css/amazeui.min.css">
	<link rel="stylesheet" href="${ctx}/assets/css/admin.css">
	<link rel="stylesheet" href="${ctx}/assets/css/app.css">
	<%@ include file="/jsp/jslib.jsp"%>
	
	<script type="text/javascript">
	PAGE_CONFIG = {
		'MENU_INDEX' : 1,'LI_INDEX' : 1
	};
	
	</script>
	
	<script type="text/javascript" src="${ctx}/js/project.js"></script>
	<script type="text/javascript">
	$(function(){
		project.ctx = "${ctx}";
		
		if("" != $('#error').val()){
		var $modal1 = $('#my-alert');
			$modal1.modal();
		}
		
		$('#close').click(function () {
			var $modal = $('#sucess-modal');
			$modal.modal('close');
	    });
	    
	    $('#btn_ok').click(function () {
	    	var $modal = $('#sucess-modal');
	    	project.projectApproval();
	    	$modal.modal('close');
	    });
	    
	    $('#projectApproval').click(function () {
	    	
	    	//先校验
	    	if(""==$("#offer").val()){
				$('#errorMsg').html("报价必填！");
				var $modal1 = $('#my-alert');
				$modal1.modal();
				return;
			}
	    	
	    	//校验待办是否已经完成
	    	var isSsuccess = true;
	    	$("input[name$='isDone']").each(function(){
	    		if($(this).val() == 'N') {
					isSsuccess = false;
	    			$('#errorMsg').html("需完成全部待办事项！");
					var $modal1 = $('#my-alert');
					$modal1.modal();
			    	return false;
	    		}
	    	});
	    	
    		if(isSsuccess == true) {
    			var $modal = $('#sucess-modal');
				$modal.modal();
    		}
	    });

	    $('#revokeClose').click(function () {
			var $modal = $('#revoke-modal');
			$modal.modal('close');
	    });
	    
	    $('#revokeBtn_ok').click(function () {
	    	var $modal = $('#revoke-modal');
	    	project.revokeProject();
	    	$modal.modal('close');
	    });
	    
	    $('#revokeProject').click(function () {
	    	var $modal = $('#revoke-modal');
			$modal.modal();
	    });
		
	});
	 
	function getPartnerProductAndContact(o) {
		//查询该供应商产品
		$.ajax({
	        type: "POST",
	        async:false,
	        url: '${ctx}/partner/product/getlistbycompanyid',
	        data : {
	        	"partnerCompanyId" : o.find("option:selected").val(),
	        },
	        success: function(list) {
	        	var select = o.parent().next().children("#partnerProductId")[0];
	        	$(select).html("");
	        	
	        	var option = '<option value="">请选择</option>';
	        	$(select).append(option);
	        	
	        	$.each(list, function(index, value){
					var id     = value.partnerProductId;
					var name   = value.partnerProductName;
					var option = '<option value="' + id + '">' + name + '</option>';
					$(select).append(option);
	        	});
	        }
	    });
		
		//查询该供应商联系人
		$.ajax({
	        type: "POST",
	        async:false,
	        url: '${ctx}/partner/contact/getlistbycompanyid',
	        data : {
	        	"partnerCompanyId" : o.find("option:selected").val(),
	        },
	        success: function(list) {
	        	var select = o.parent().next().next().children("#partnerContactId")[0];
	        	$(select).html("");
	        	
	        	var option = '<option value="">请选择</option>';
	        	$(select).append(option);
	        	
	        	$.each(list, function(index, value){
					var id     = value.contactId;
					var name   = value.contactName;
					var option = '<option value="' + id + '">' + name + '</option>';
					$(select).append(option);
	        	});
	        }
	    });
	}
	</script>
	
</head>

<body>
<%@ include file="/jsp/banner.jsp"%>
<div class="am-cf admin-main">
<%@ include file="/jsp/menu.jsp"%>
	
<!-- content start -->
<div class="admin-content">
	<div class="admin-content-body">
    
    <div class="am-cf am-padding am-padding-bottom-0">
      <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">编辑售前项目详细信息</strong></div>
    </div>
    <hr>
    
    <div class="am-tabs am-margin" data-am-tabs>
		<ul class="am-tabs-nav am-nav am-nav-tabs"></ul>
		<div class="am-tabs-bd">
	        <div class="am-tab-panel am-fade am-in am-active" id="tab1">
	          
	          <form id="beforProjectForm" action="${ctx}/project/updateBeforeProject" method="post" class="am-form" >
	            
	            <div class="am-g am-margin-top ">
	              <div class="am-u-sm-4 am-u-md-2 am-text-right">项目名称</div>
	              <div class="am-u-sm-8 am-u-md-4 am-u-end col-end">${project.projectName}</div>
	            </div>
	
				<div class="am-g am-margin-top">
	              <div class="am-u-sm-4 am-u-md-2 am-text-right">客户公司</div>
	              <div class="am-u-sm-8 am-u-md-10">
	                <select data-am-selected="{btnSize:'sm'}" disabled>
	                	<option value="">请选择</option> 
	                  	<c:forEach items="${customerCompanyList }" var = "customerCompany" >
	           				<option value="${customerCompany.customerCompanyId }" <c:if test="${customerCompany.customerCompanyId == project.customerCompanyId}">selected</c:if>>${customerCompany.customerCompanyName }</option>
	           			</c:forEach>
	                </select>
	              </div>
	            </div>
	          
				<div class="am-g am-margin-top">
				  <div class="am-u-sm-4 am-u-md-2 am-text-right" disabled>客户负责人</div>
				  <div class="am-u-sm-8 am-u-md-10">
				    <select data-am-selected="{btnSize:'sm'}" name="customerId" id ="customerId">
				    	<option value="">请选择</option> 
						<c:forEach items="${customerList }" var = "customer" >
							<option value="${customer.customerId }" <c:if test="${customer.customerId == project.customerId}">selected</c:if>>${customer.customerName }</option>
						</c:forEach>
				    </select>
				  </div>
				</div>
	          
				<div class="am-g am-margin-top">
				  <div class="am-u-sm-4 am-u-md-2 am-text-right">接单部门</div>
				  <div class="am-u-sm-8 am-u-md-10">
				    <select data-am-selected="{btnSize:'sm'}" >
				    	<option value="">请选择</option> 
						<c:forEach items="${companyList }" var = "company" >
							<option value="${company.companyId }" <c:if test="${company.companyId == project.companyId}">selected</c:if>>${company.companyName }</option>
						</c:forEach>
				    </select>
				  </div>
				</div>
	
				<div class="am-g am-margin-top">
				  <div class="am-u-sm-4 am-u-md-2 am-text-right">负责人</div>
				  <div class="am-u-sm-8 am-u-md-10">
				    <select data-am-selected="{btnSize: 'sm'}" <c:if test="${permission == 0}">disabled</c:if> name="" id="" >
				    	<option value="">请选择</option> 
						<c:forEach items="${staffList }" var = "staff" >
							<option value="${staff.staffId }" <c:if test="${staff.staffId == project.staffId}">selected</c:if>>${staff.staffName }</option>
						</c:forEach>
				    </select>
				  </div>
				</div>
				
				<div class="am-g am-margin-top">
	              <div class="am-u-sm-4 am-u-md-2 am-text-right">项目进度</div>
	              <div class="am-u-sm-8 am-u-md-4 am-u-end col-end">
	                <%-- <input type="text" class="am-input-sm" name="projectSchedule" id="projectSchedule"  value="${project.projectSchedule}"> --%>
	              	<select data-am-selected="{btnSize:'sm'}" name="projectSchedule" id="projectSchedule" >
				    	<option value="">请选择</option>
				    	 <c:forEach items="${projectSchedule }" var = "projectSchedule" >
							<option value="${projectSchedule.value}" <c:if test="${projectSchedule.value == project.projectSchedule}">selected</c:if>>${projectSchedule.value}</option>
						</c:forEach>
						<%-- <option value="10%销售机会" <c:if test="${ '10%销售机会'== project.projectSchedule}">selected</c:if>>10%销售机会</option>
						<option value="20％方案测试" >20％方案测试</option>
						<option value="30%报价" >30%报价</option>
						<option value="40%客户采购流程" >40%客户采购流程</option>
						<option value="50％招投标阶段" >50％招投标阶段</option>
						<option value="60%签订合同" >60%签订合同</option>
						<option value="70%交货安装" >70%交货安装</option>
						<option value="80%收款" >80%收款</option>
						<option value="90%运维期" >90%运维期</option>
						<option value="100%项目完成" >100%项目完成</option> --%>
						
				    </select>
	              </div>
	            </div>
	
	            <div class="am-g am-margin-top">
	              <div class="am-u-sm-4 am-u-md-2 am-text-right">报价</div>
	              <div class="am-u-sm-8 am-u-md-4 am-u-end col-end">
	                <input type="text" class="am-input-sm" name="offer" id="offer"  value="${project.offer}" required placeholder="请输入数字" min="0">
	              </div>
	              <div class="am-hide-sm-only am-u-md-6">*必填</div>
	            </div>
		            
	            <div class="am-g am-margin-top">
	              <div class="am-u-sm-4 am-u-md-2 am-text-right">预定成交日期</div>
	              <div class="am-u-sm-8 am-u-md-4 am-u-end col-end">
	                <input type="date" class="am-form-field am-input-sm" name="expectedDealDate" id="expectedDealDate"  placeholder="时间"
	                 value="<fmt:formatDate value='${project.expectedDealDate}' pattern='yyyy-MM-dd'/>">
	              </div>
	            </div>
				
	            <div class="am-g am-margin-top">
		            <div class="am-u-sm-4 am-u-md-2 am-text-right"> 供应商 </div>
		            
					<div class="am-u-sm-8 am-u-md-10">
	            		<button type="button" onclick="project.addPartnerCompany(this)" class="am-btn am-btn-default"><span class="am-icon-plus"></span>添加供应商</button>
						<table id="partnerCompanyTable" class="am-table am-table-bd am-table-striped admin-content-table">
							<thead>
					            <tr>
					              <td hidden>ID</td>
					              <th></th>
					              <th></th>
					              <th></th>
					              <th></th>
					            </tr>
				            </thead>
							<tbody>
								<c:forEach var="partner" items="${partnerList}" varStatus="s">
									<tr>
										<td hidden>${partner.partnerCompanyId}</td>
										<td width="40%">
											<!-- 供应商 -->
											<select id="partnerCompanyId" name="partnerCompanyId" onchange="getPartnerProductAndContact($(this))">
												<option value="">请选择</option>
												<c:forEach items="${partnerCompanyList}" var = "partnerCompany" >
													<option value="${partnerCompany.partnerCompanyId}" <c:if test="${partner.partnerCompanyId == partnerCompany.partnerCompanyId}">selected</c:if>>${partnerCompany.partnerCompanyName}</option>
												</c:forEach>
											</select>
										</td>
										<td width="25%">
											<!-- 产品/服务 -->
											<select id="partnerProductId" name="partnerProductId">
												<option value="">请选择</option>
												<c:forEach items="${partner.partnerProductList}" var = "partnerProduct" >
													<option value="${partnerProduct.partnerProductId}" <c:if test="${partner.partnerProductId == partnerProduct.partnerProductId}">selected</c:if>>${partnerProduct.partnerProductName}</option>
												</c:forEach>
											</select>
										</td>
										<td width="25%">
											<!-- 供应商联系人 -->
											<select id="partnerContactId" name="partnerContactId">
												<option value="">请选择</option>
												<c:forEach items="${partner.partnerContactList}" var = "partnerContact" >
													<option value="${partnerContact.contactId}" <c:if test="${partner.partnerContactId == partnerContact.contactId}">selected</c:if>>${partnerContact.contactName}</option>
												</c:forEach>
											</select>
										</td>
										<td width="10%">
											<button name="removeTr" onclick="$(this).parent().parent().remove();" type="button" class="am-btn am-btn-default am-btn-sm">删除</button>
										</td>
									</tr>
								</c:forEach>
							</tbody>
						</table>
					</div>
				</div>
				
				<div class="am-g am-margin-top">
		            <div class="am-u-sm-4 am-u-md-2 am-text-right">待办事项 </div>
		            <div class="am-u-sm-8 am-u-md-10">
		            	<button type="button" onclick="project.addLog(this)" class="am-btn am-btn-default"><span class="am-icon-plus"></span>添加日志 </button>
			             <table id="logTable" class="am-table am-table-bd am-table-striped admin-content-table">
				            <thead>
					            <tr>
					              <td hidden>ID</td>
					              <th>待办事项</th>
					              <th><div class="am-u-md-8">待办日期</div></th>
					              <th>是否完成</th>
					            </tr>
				            </thead>
				            <tbody>
				            	<c:forEach var="projectLog" items="${projectLogList}" varStatus="s">
				            		<tr x-update>
				            			<!-- 日志id -->
				            			<td hidden><input type="hidden" name="logId" value="${projectLog.logId}" /></td>
				            			<!-- 待办事项 -->
				            			<td>
				            				<textarea id="todoSomething" name="todoSomething" class="am-input-sm" rows="1">${projectLog.todoSomething}</textarea>
				            			</td>
						            	<!-- 待办日期 -->
						            	<td>
											<div class="am-u-md-8">
						              			<input type="date" name="todoDate" class="am-form-field am-input-sm" 
						              				placeholder="时间" value="<fmt:formatDate value='${projectLog.todoDate}' pattern='yyyy-MM-dd'/>">
						              		</div>
										</td> 
						            	<!-- 是否完成 -->
						            	<td>
											<label class="am-checkbox">
										      <input type="checkbox" name="isDone" value="${projectLog.isDone}" ${projectLog.isDone == 'Y' ? 'checked' : ''} data-am-ucheck>
										    </label>
										</td>
				            		</tr>
				            	</c:forEach>
				            </tbody>
			          	</table>
			        </div>
				</div>
				
				<div class="am-g am-margin-top">
		            <div class="am-u-sm-4 am-u-md-2 am-text-right">已完成事项 </div>
		            <div class="am-u-sm-8 am-u-md-10">
			             <table id="logTable2" class="am-table am-table-bd am-table-striped admin-content-table">
				            <thead>
					            <tr>
					              <td hidden>ID</td>
					              <th>完成事项</th>
					              <th><div class="am-u-md-8">完成日期</div></th>
					            </tr>
				            </thead>
				            <tbody>
				            	<c:forEach var="projectLog" items="${projectLogList2}" varStatus="s">
				            		<tr x-update>
				            			<!-- 完成事项 -->
				            			<td>
				            				<textarea id="todoSomething" name="todoSomething" class="am-input-sm" rows="1">${projectLog.todoSomething}</textarea>
				            			</td>
						            	<!-- 完成日期 -->
						            	<td>
											<div class="am-u-md-8">
						              			<input type="date" name="todoDate" class="am-form-field am-input-sm" 
						              				placeholder="时间" value="<fmt:formatDate value='${projectLog.todoDate}' pattern='yyyy-MM-dd'/>" disabled>
						              		</div>
										</td> 
				            		</tr>
				            	</c:forEach>
				            </tbody>
			          	</table>
			        </div>
				</div>
	      		
	      		<div class="am-g am-margin-top">
	      			<div class="am-u-sm-4 am-u-md-2 am-text-right">备注</div>
		            <div class="am-u-sm-8 am-u-md-10">
	      				<textarea id="note" name="note" class="am-input-sm" rows="5">${project.note}</textarea>
      				</div>
	      		</div>
	      		
	      		<input type="hidden" name="projectId" id="projectId" value="${project.projectId}">
				<input type="hidden" name="logUpdateListJson" />
			    <input type="hidden" name="logInsertListJson" />
			    <input type="hidden" name="partnerUpdateListJson" />
			    <input type="hidden" name="partnerInsertListJson" />
			    
	          </form>
	        
	        </div>
		</div>
		<div class="am-margin">
	     	<button type="button" class="am-btn am-btn-secondary" onclick="project.submitBeforeForm();" >提交保存</button>
	      	<button type="button" class="am-btn am-btn-secondary" id="projectApproval" >确定立项</button>
	      	<button type="button" class="am-btn am-btn-secondary" id="revokeProject" >废除</button>
		</div>
		<!-- 错误提示信息 -->
		<input type="hidden" id="error" value="${error}"/>
		<div class="am-modal am-modal-alert" tabindex="-1" id="my-alert">
		  <div class="am-modal-dialog">
		    <div class="am-modal-hd">错误信息</div>
		    <div id ="errorMsg" class="am-modal-bd">
		     ${error}
		    </div>
		    <div class="am-modal-footer">
		      <span class="am-modal-btn">确定</span>
		    </div>
		  </div>
		</div>
		
		<div class="am-modal am-modal-no-btn" tabindex="-1" id="sucess-modal">
		    <div class="am-modal-dialog">
		        <div class="am-modal-hd" id="titleMsg">确定立项？</div>
		        <div class="am-modal-bd">
		            <button type="button" class="am-btn am-btn-primary" id="close"> 取消 </button>
		            <button type="button" class="am-btn am-btn-primary" id="btn_ok" > 确定 </button>
		        </div>
		    </div>
		</div>
		
		<div class="am-modal am-modal-no-btn" tabindex="-1" id="revoke-modal">
		    <div class="am-modal-dialog">
		        <div class="am-modal-hd" id="titleMsg">确定废除“${project.projectName}”项目么？</div>
		        <div class="am-modal-bd">
		            <button type="button" class="am-btn am-btn-primary" id="revokeClose"> 取消 </button>
		            <button type="button" class="am-btn am-btn-primary" id="revokeBtn_ok" > 确定 </button>
		        </div>
		    </div>
		</div>
		
    </div>
    
	</div>
</div>
<!-- content end -->

</div>
<script id="tpl-table2" type="text/x-handlebars-template">
	<tr x-insert>
		<td hidden><input type="hidden" name="logId" value="" /></td>
		<td>
			<textarea id="todoSomething" name="todoSomething" class="am-input-sm" rows="1"></textarea>
		</td>
		<td>
			<div class="am-u-md-8">
				<input type="date" name="todoDate" class="am-form-field am-input-sm" placeholder="时间" value="">
			</div>
		</td> 
		<td>
			<label class="am-checkbox">
				<input type="checkbox" name="isDone" value="" data-am-ucheck>
			</label>
		</td> 
	</tr> 
</script>
<script id="tpl-table3" type="text/x-handlebars-template">
<tr>
	<td hidden>${partnerCompanyId}</td>
	<td>
		<select id="partnerCompanyId" name="partnerCompanyId" onchange="getPartnerProductAndContact($(this))">
			<option value="">请选择供应商</option>
			<c:forEach items="${partnerCompanyList}" var = "partnerCompany" >
				<option value="${partnerCompany.partnerCompanyId}">${partnerCompany.partnerCompanyName}</option>
			</c:forEach>
		</select>
	</td>
	<td width="25%">
		<!-- 产品/服务 -->
		<select id="partnerProductId" name="partnerProductId">
			<option value="">请选择</option>
		</select>
	</td>
	<td width="25%">
		<!-- 供应商联系人 -->
		<select id="partnerContactId" name="partnerContactId">
			<option value="">请选择</option>
		</select>
	</td>
	<td>
		<button name="removeTr" onclick="$(this).parent().parent().remove();" type="button" class="am-btn am-btn-default am-btn-sm">删除</button>
	</td>
</tr>
</script>
</body>
</html>